﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.Model;
using YouHP.Utility;
using YouHP.Model.Group;
using YouHP.BLL;
using System.Web;

namespace YouHP.Pages.Group
{
    public class GroupListBySearch:BasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (ErrorCount > 0) return;

            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected GroupSearchCondition cond = new GroupSearchCondition();
        protected List<GroupInfo> groupListLatest;
        protected List<GroupInfo> groupListHotest;

        protected List<GroupInfo> GroupList;
        protected int pageIndex = 1;
        protected int pageSize = 6;
        protected int pageCount = 0;
        protected int groupCount = 0;
        protected string PagingString;
        protected int n = 18;


        protected void HandleCommon()
        {
            this.SetTitle("病友个人圈");
            this.AddLocation("病友个人圈", "/Group/Index.aspx");
            this.AddLocation("搜索结果", "/Group/GroupListBySearch.aspx");
            groupListLatest = GroupBLL.Instance.GetGroupListLatest(n);
            groupListHotest = GroupBLL.Instance.GetGroupListHotest(n);
        }

        protected void HandlePost()
        {
        }

        protected void HandleGet()
        {
            cond.GroupName = RequestTools.GetQueryString("groupName");
            cond.ProvinceId = RequestTools.GetQueryInt("provinceId", 0);
            cond.CityId = RequestTools.GetQueryInt("cityId", 0);
            cond.DiseaseId = RequestTools.GetQueryInt("diseaseId", 0);
            cond.SymptomId = RequestTools.GetQueryInt("symptomId", 0);
            cond.TreatmentId = RequestTools.GetQueryInt("treatmentId", 0);
            //排序字段
            cond.OrderByField = 1;

            pageIndex = RequestTools.GetQueryInt("page", 1);
            GroupList = GroupBLL.Instance.GetGroupListBySearch(pageSize, pageIndex, cond, out groupCount);

            pageCount = Utils.GetPageCount(groupCount, pageSize);
            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PagingString = this.GetPagingString(pageIndex, pageCount,
                "/Group/GroupListBySearch.aspx" + query, "page");

        }

        protected string GetCondStr(GroupSearchCondition cond)
        {
            string condStr = String.Empty;
            bool firstCond = true;

            if (cond == null)
            {
                return string.Empty;
            }

            if (cond.DiseaseId > 0)
            {
                condStr += DiseaseBLL.Instance.GetDiseaseInfo(cond.DiseaseId).Name;
                firstCond = false;
            }

            if (cond.SymptomId > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                }
                condStr += SymptomBLL.Instance.GetSymptomInfo(cond.SymptomId).Name;
                firstCond = false;
            }

            if (cond.TreatmentId > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                }
                condStr += TreatmentBLL.Instance.GetTreatmentInfo(cond.TreatmentId).Name;
                firstCond = false;
            }

            if (cond.ProvinceId > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                }
                condStr += DistrictBLL.Instance.GetDistrict(cond.ProvinceId).Name;
                firstCond = false;
            }

            if (cond.CityId > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                }
                condStr += DistrictBLL.Instance.GetDistrict(cond.CityId).Name;
                firstCond = false;
            }

            if (cond.GroupName.Length > 0)
            {
                if (!firstCond)
                {
                    condStr += " + ";
                }
                condStr += cond.GroupName;
            }

            return condStr;
        }

    }
}
